import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class Page2 extends StatefulWidget {
  @override
  _Page2State createState() => _Page2State();
}

class _Page2State extends State<Page2> {
// 存储每天的事件
  Map<DateTime, List<String>> _events = {};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('时间轴日程表'),
      // ),
      body: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
            image: AssetImage('assets/img_1.png'), // 请替换为您的背景图像路径
            fit: BoxFit.cover,
          )),
          child: _buildTimeline()),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _showEventDialog();
        },
        child: Icon(Icons.add),
      ),
    );
  }

  // 构建时间轴
  Widget _buildTimeline() {
    List<DateTime> sortedDates = _events.keys.toList()..sort();
    print(_events.length);
    return ListView.builder(
      itemCount: sortedDates.length,
      itemBuilder: (context, index) {
        DateTime date = sortedDates[index];
        List<String>? dayEvents = _events[date];

        return Container(
          margin: EdgeInsets.only(top: 20),
          color: Colors.green,
          child: Column(
            children: [
              ListTile(
                title: Text(
                  DateFormat('yyyy-MM-dd HH:mm:ss').format(date),
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              for (String event in dayEvents!)
                ListTile(
                  title: Text(event),
                ),
            ],
          ),
        );
      },
    );
  }

  // 显示添加事件的对话框
  void _showEventDialog() {
    DateTime currentDate = DateTime.now();
    TextEditingController controller = new TextEditingController();
    String currentDateString =
        "${currentDate.year}-${currentDate.month}-${currentDate.day}";

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Add event'),
          content: TextField(
            controller: controller,
            decoration: InputDecoration(
              labelText: 'Add event here...',
            ),
            onSubmitted: (event) {
              setState(() {
                if (_events.containsKey(currentDate)) {
                  _events[currentDate]?.add(event);
                } else {
                  _events[currentDate] = [event];
                }
                print(_events.keys.length);
              });
              Navigator.of(context).pop();
            },
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('cancel'),
            ),
            TextButton(
              onPressed: () {
                setState(() {
                  if (_events.containsKey(currentDate)) {
                    _events[currentDate]?.add(controller.text);
                  } else {
                    _events[currentDate] = [controller.text];
                  }
                });
                Navigator.of(context).pop();
              },
              child: Text('save'),
            ),
          ],
        );
      },
    );
  }
}
